package com.tianxin.workbao.workBao.Activity;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.appcompat.content.res.AppCompatResources;
import androidx.appcompat.widget.Toolbar;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.tianxin.workbao.workBao.R;
import com.tianxin.workbao.workBao.adapter.SelectCarAdapter;
import com.tianxin.workbao.workBao.bean.CarFinance;
import com.tianxin.workbao.workBao.bean.CarInformation;
import com.tianxin.workbao.workBao.common.Constant;
import com.tianxin.workbao.workBao.dataProvide.CarFinanceProvide;
import com.tianxin.workbao.workBao.dataProvide.CarProvide;
import com.tianxin.workbao.workBao.holder.SelectCarHolder;
import com.tianxin.workbao.workBao.listener.MyPopupWindowListener;
import com.tianxin.workbao.workBao.listener.UpdateListViewListener;
import com.tianxin.workbao.workBao.utils.common.StringUtils;
import com.tianxin.workbao.workBao.utils.common.ToastUtils;

import java.util.List;

/**
 * Created by hacker on 2017/6/22.
 */

public class CarMoneySearchActivity extends BaseActivity implements View.OnClickListener, UpdateListViewListener, MyPopupWindowListener {

    private TextView carInformationTV;
    private EditText searchContentET;
    private TextView residual_moneyTV;
    private ImageView clearSearchContentBtn;
    private TextView inMoneyTV;
    private TextView outMoneyTV;
    private TextView marginMoneyTV;
    private CarProvide carProvide;
    private List<CarInformation> carInformationList;
    private CarFinanceProvide carFinanceProvide;
    private PopupWindow popupWindow;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.car_finance_search);
        super.initStatusBar(true);

        initData();
        initView();
        initEvent();
        bindView();
    }

    @Override
    public View onCreateView(String name, Context context, AttributeSet attrs) {
        return super.onCreateView(name, context, attrs);
    }

    public void initView() {
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        mToolbar.setTitle(getResources().getString(R.string.car_finance_search));
        setSupportActionBar(mToolbar);
        mToolbar.setNavigationIcon(AppCompatResources.getDrawable(this, R.drawable.ic_action_back_24dp));
        carInformationTV = (TextView) findViewById(R.id.car_information);
        searchContentET = (EditText) findViewById(R.id.et_search_content);
        residual_moneyTV = (TextView) findViewById(R.id.residual_money);
        inMoneyTV = (TextView) findViewById(R.id.in_money);
        outMoneyTV = (TextView) findViewById(R.id.out_money);
        marginMoneyTV = (TextView) findViewById(R.id.marin_money);
        clearSearchContentBtn = (ImageView) findViewById(R.id.clear_search_content);
    }

    @Override
    public void initData() {

    }

    @Override
    public void initEvent() {
        //navigationIcon event
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CarMoneySearchActivity.this.finish();
            }
        });
        clearSearchContentBtn.setOnClickListener(this);
        searchContentET.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                String queryString = editable.toString();
                if (queryString.length() > 0) {
                    clearSearchContentBtn.setVisibility(View.VISIBLE);
                } else {
                    clearSearchContentBtn.setVisibility(View.GONE);
                }
                if (!StringUtils.isNumber(queryString)) {
                    showMessage("输入错误");
                    carInformationTV.setText("");
                    carInformationTV.setFocusable(true);
                    return;
                }
                if (StringUtils.isHasChineseChar(queryString)) {
                    //get car information by plateNumber
                    if (StringUtils.isPlateNumber(queryString) == true) {
                        //not do nothing
                    } else {
                        showMessage("车牌号错误");
                        carInformationTV.setText("");
                        carInformationTV.setFocusable(true);
                    }
                } else {
                    if (queryString.length() >= 3 && queryString.length() <= 5) {
                        //get car by plateNumber like query string
                        if (queryString.startsWith("800")) {
                            return;
                        } else {
                            showPopupWindow(queryString);
                        }
                    } else if (queryString.length() > 5 && queryString.length() == 8) {
                        if (StringUtils.isNumber(queryString) == true) {
                            //get cars by gas card number
                            getCarProvide().getcarBycpunumber(queryString);
                            // get car finance residual money in the interface
                        } else {
                            showMessage("输入错误");
                            carInformationTV.setText("");
                            carInformationTV.setFocusable(true);
                        }
                    } else if (queryString.length() > 8) {
                        showMessage("输入错误");
                        carInformationTV.setText("");
                        carInformationTV.setFocusable(true);
                    }
                }
            }
        });
    }

    /**
     * show the select car popup window
     */
    private void showSelectCarPopupWindow() {
        popupWindow = new PopupWindow();
        SelectCarAdapter selectCarAdapter = new SelectCarAdapter(carInformationList, this, this);
        SelectCarHolder selectCarHolder = new SelectCarHolder(this, this, selectCarAdapter);
        popupWindow.setContentView(selectCarHolder.getContentView());
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setHeight(300);
        popupWindow.setWidth(WindowManager.LayoutParams.MATCH_PARENT);
        popupWindow.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.write)));
        popupWindow.setAnimationStyle(R.style.my_popup_animation_style);
        popupWindow.showAtLocation(mToolbar, Gravity.BOTTOM, 0, 0);
    }

    /**
     * indistinct get car  like queryString
     *
     * @param queryString
     */
    private void showPopupWindow(String queryString) {
        getCarProvide().getCarinformationsLikeQueryString(queryString);
    }

    @Override
    public void bindView() {
        carInformationTV.setText("");
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

    @Override
    public void onClick(View view) {
        int viewId = view.getId();
        switch (viewId) {
            case R.id.save_fab:
                break;
            case R.id.clear_search_content:
                searchContentET.setText("");
                searchContentET.setFocusable(true);
                break;
        }
    }


    @Override
    public void showMessage(String msg) {
        ToastUtils.showLong(msg);
    }

    @Override
    public void showProgress() {
        getSweetAlertDialog(this).showDialog("", 1200);
    }

    @Override
    public void hideProgress() {
        getSweetAlertDialog(this).dismiss();
    }

    @Override
    public void onRefreshData(Object result) {
        if (result instanceof List) {
            if (((List) result).size() > 0) {
                if (((List) result).get(0) instanceof CarInformation) {
                    carInformationList = (List<CarInformation>) result;
                    showSelectCarPopupWindow();
                }
            }
        } else {
            if (result instanceof CarFinance) {
                CarFinance finance = (CarFinance) result;
                carInformationTV.setText(finance.getPlatenumbers());
                inMoneyTV.setText("￥" + finance.getAllinputmoney());
                outMoneyTV.setText("￥" + finance.getAlloutputmoney());
                residual_moneyTV.setText("￥" + finance.getResidualmoney());
                marginMoneyTV.setText("￥" + finance.getMarginmoney());
            }
        }
    }


    @Override
    public void onLoadMoreData(Object result) {

    }

    public CarProvide getCarProvide() {
        if (carProvide == null) {
            carProvide = new CarProvide(this, this);
        }
        return carProvide;
    }


    public CarFinanceProvide getCarFinanceProvide() {
        if (carFinanceProvide == null) {
            carFinanceProvide = new CarFinanceProvide(this, this);
        }
        return carFinanceProvide;
    }

    @Override
    public void OnMyClick(Integer selectType, Integer id, String information) {
        switch (selectType) {
            case Constant.SELECT_TYPE_CAR:
                if (popupWindow != null) {
                    popupWindow.dismiss();
                }
                getCarFinanceProvide().getCarfinance(information);
                break;
        }
    }
}
